package com.springboot42.service;

import com.springboot42.dao.CityMapper;
import com.springboot42.dao.MayorMapper;
import com.springboot42.pojo.City;
import com.springboot42.pojo.Mayor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Service
public class CityServiceImpl implements CityService{

    @Autowired
    MayorMapper mayorMapper;

    @Autowired
    CityMapper cityMapper;

    //查询所有市长及对应城市信息
    @Override
    public List<Mayor> findAllMayor() {
        return mayorMapper.findAllMayor();
    }

    //通过姓名查询市长及对应城市信息
    @Override
    public List<Mayor> searchMayorByName(String name) {
        return mayorMapper.searchMayorByName(name);
    }

    //级联添加新市长及对应城市信息
    @Transactional
    @Override
    public int addMayor(Mayor mayor) {
        //        事物：多个操作同时完成，一个有问题，原来完成的操作要回滚
        int result=cityMapper.addCity(mayor.getCity());
        if(result>0){
            return mayorMapper.addMayor(mayor);
        }
        return 0;
    }

    //删除操作，分别对两个单表进行删除
    @Override
    public int deleteMayorById(int cid){
        cityMapper.deleteCityById(cid);
        mayorMapper.deleteMayorById(cid);
        return 0;
    }

    //级联更新市长及对应城市信息
    @Override
    public int updateMayor(Mayor mayor) {
        int result=cityMapper.updateCity(mayor.getCity());
        if(result>0){
            return mayorMapper.updateMayor(mayor);
        }
        return 0;
    }

}
